using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Forecast.Business;

public partial class controls_grids_Holdings : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["sortColumn"] = " ";
            ViewState["sortDirection"] = " ";
        }

        if (string.IsNullOrEmpty(Request["traderid"])) throw new Exception("Missing argument - don't know which trader to display.");

        Activity activity = new Activity(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        DataTable table = activity.GetPortfolio(Int32.Parse(Request["traderid"]));
        DataView view = new DataView(table);
        view.Sort = "[" + Portfolio.Columns.TotalValue.ToString() + "] DESC";

        GridView1.DataSource = view;
        GridView1.DataBind();

        if (GridView1.Rows.Count == 0)
        {
            _noGridText.Text = string.Format("<b>{0}</b><br/>", "No stocks in portfolio yet.");
            Panel1.Visible = false;
        }
        else
        {
            _noGridText.Visible = false;
        }
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataView view = GridView1.DataSource as DataView;

        if (ViewState["sortColumn"].ToString() == e.SortExpression.ToString())
        {
            if ("DESC" == ViewState["sortDirection"].ToString())
            {
                ViewState["sortDirection"] = "ASC";
            }
            else
            {
                ViewState["sortDirection"] = "DESC";
            }
        }
        else
        {
            ViewState["sortColumn"] = e.SortExpression.ToString();
            ViewState["sortDirection"] = "DESC";
        }

        view.Sort = ViewState["sortColumn"].ToString() + " " + ViewState["sortDirection"].ToString();

        GridView1.DataSource = view;
        GridView1.DataBind();
    }

    protected string GetChangeFormatted(object change)
    {
        double fChange = (double)change;
        return string.Format("<span style=\"color:{0};\">{1}%</span>",
            (fChange >= 0 ? "green" : "red"),
            fChange);
    }

    protected string GetFormattedResult(object resultAsObject)
    {
        int result = Int32.Parse(resultAsObject.ToString());
        string weather = GetWeatherIcon(result);
        return string.Format(weather + "<span style=\"color:{0};\">{1}</span>",
            (result >= 0 ? "green" : "red"),
            result);
    }

    private string GetWeatherIcon(int result)
    {
        if (result > 100)
            return "<img alt=\"Profit is looking good!\" src=\"images/silk/weather_sun.png\">";
        if (result > -100)
            return "<img alt=\"In balance.\" src=\"images/silk/weather_cloudy.png\">";
        if (result > -500)
            return "<img alt=\"A small loss so far.\" src=\"images/silk/weather_clouds.png\">";
        if (result > -1500)
            return "<img alt=\"This ticker is turning bad.\" src=\"images/silk/weather_rain.png\">";
        return "<img alt=\"Ayay, caramba! The results are horrible!\" src=\"images/silk/weather_lightning.png\">";
    }
}
